package com.jackrain.nea.fc.dl.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.WmsBackWriteSaveCmd;
import com.jackrain.nea.fc.dl.mapper.WmsBackWriteMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * WMS回写
 *
 * @author xuxinshuai
 * @date 2018/05/05
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class WmsBackWriteSaveCmdImpl extends CommandAdapter implements WmsBackWriteSaveCmd {


    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        WmsBackWriteMapper wmsBackWriteMapper = ApplicationContextHandle.getBean(WmsBackWriteMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        log.info("WmsBackWriteSaveCmdImpl-->param-->{}",param);
        String tablename = param.getString("tablename");

        Object[] list = (Object[]) param.getJSONArray("IDS").toArray();
        StringBuilder sb = new StringBuilder();
        for (Object num : list) {
            sb.append(num).append(",");
        }

        String iswms = null;
        int updateRets = 0;
        if (tablename.equals("DL_B_PUR_ORDER")) {
            updateRets = wmsBackWriteMapper.updatorderwmsState(sb.toString());
        } else if (tablename.equals("DL_B_TRAN")) {//提交
            updateRets = wmsBackWriteMapper.updatwmssubmitState(sb.toString());
        } else if (tablename.equals("DL_B_TRAN_OUT")) {//发出
            updateRets = wmsBackWriteMapper.updatwmssendState(sb.toString());
        } else if (tablename.equals("DL_B_PUR_IN_ACCEPT")) {//采购入库验收
            updateRets = wmsBackWriteMapper.updatpurinwmsState(sb.toString());
        } else if (tablename.equals("DL_B_PUR_RET_SEND")) {//采购退货发出
            updateRets = wmsBackWriteMapper.updatpurretwmsState(sb.toString());
        } else if (tablename.equals("DL_B_TRAN_OUT_SEND")) {//调拨出库发出
            updateRets = wmsBackWriteMapper.updattranoutwmsState(sb.toString());
        } else if (tablename.equals("DL_B_TRAN_IN_ACCEPT")) {//调拨入库验收
            updateRets = wmsBackWriteMapper.updattraninwmsState(sb.toString());
        }
        if (updateRets <= 0) {
            throw new NDSException("WMS标示更新失败！");
        }
        vh.put("code", 0);
        vh.put("message", "更新成功！");
        return vh;

    }


}
